# -*- coding: utf-8 -*-


import model
from training import train2
import alignment
import char_inputs
import re



    
def convert(conv_lookup, text, join = True):
    """ Converts a sentence (text), preserving punctuation and capitalization.
    Returns the converted sentence and a dictionary of the converted words.
    conv_lookup is a function that returns a converter object that is best suited to deal with the supplied token
    """
    converted_list = []
    conv_dict = {}
    tokens = bounds.split(text)   
    for token in tokens:
        if token.isalnum()and not digits.match(token):
            mask = mask_from_case(token)
            #print mask,'-'+token+'-'
            conv = conv_lookup(token)
            converted = conv.convert(token)
            if not converted: converted = token
            converted = case_from_mask(converted,  mask)
            conv_dict[converted]=token
            converted_list.append(converted)
        else:
            converted_list.append(token)
    if join:
        return (u''.join(converted_list),conv_dict)
    else:
        return (converted_list,conv_dict)
        
        

class Converter(object):
    def __init__(self, data_file, greek_file='greekwords.txt', grkl_file='wordlist2.txt' ,
                 align=alignment.align3, split=alignment.split_smart):
        print align
        print split
        try:
            self.model= model.read_model(data_file)
            print 'Loaded model from',data_file


        except:
            print "Model not found, training..."
            self.model = train2(greek_file,grkl_file, align, split) 
            model.dump_model(self.model, data_file)  
        
        print "optimizing..."
        self.model._optimize()
        
        print self.model
        self.parts = self.model.observations
        self.parts.sort(key=len, reverse=True)
        self.split = split
        self.align = align
            
    def convert(self,word, join=True):
        sequence = self.split(word.lower(),self.parts, char_inputs.greeklish_split)
        
        if(sequence):
            result = self.model.viterbi(sequence)
            if join:return ''.join(result)
            else: return result
        else:
#            print 'ERROR: could not split word',word.lower()
            
            return None
    
    def probability(self,word):
        sequence = self.split(word.lower(),self.parts, char_inputs.greeklish_split)
        if(sequence):
            result = self.model.forward(sequence)
            return result
        return None
    
    def sequence(self,word):
        return self.split(word.lower(),self.parts, char_inputs.greeklish_split)
import pprint
if __name__ == '__main__':
    mask =  mask_from_case('OrEsTis')
    print case_from_mask(u'ορέστης', mask).encode('utf-8')
    conv = Converter('model_test.hmm')
    sentence = "Me lene Poph, san thn giagia mou thn Kallioph.\nAx na me legane Kybelh! - mou phgainei pio poly\nayto to 'l' me to 'h'"
    print sentence
    res=  convert(lambda x:conv, sentence)
    print res[0].encode('utf-8')
    for key in res[1]:
        print key.encode('utf-8'),':',res[1][key]
    
    